var actualites_tab = 5;

$(function(){
    var _dis_menu = false;
    var _boolPhotos = false, slider = $('#sections-slider'), nav = $('#sections-nav');
    var hash = parseInt(location.hash.slice(1)) || 0, idg = document.location.href.replace(/(.*)\&album\=([0-9]+)$/, '$2');
    
    if (hash == 4) {
        location.hash = '#5';
    }
   
    slider.codaSlider({
        crossLinking: true,
        dynamicTabsPosition: '.bn-ct-r-center-menu'
    });
    
    actualites_tab = (_num_galleries + 8) - 3;
        
    $('.coda-nav li a', nav).click(function(){
        var _texto = $.trim( $(this).attr('id') );
        switch(_texto){
            case "m-presentation":
                displayContacts(false);
                if(_boolPhotos) displayPhotos(false);
                displayPresentation(true);
                break;
            case "m-actualites":
                displayPresentation(false);
                if(_boolPhotos) displayPhotos(false);
                displayContacts(false);
                break;
            case "m-contacts":
                displayPresentation(false);
                if(_boolPhotos) displayPhotos(false);
                displayContacts(true);
                break;
            /*case "m-acess":
				displayPresentation(false);
				if(_boolPhotos) displayPhotos(false);
			break;*/
            case "m-liens":
                displayPresentation(false);
                if(_boolPhotos) displayPhotos(false);
                displayContacts(false);
                break;
            case "m-photos":
                displayPresentation(false);
                displayContacts(false);
                displayPhotos(true);
                $('li a.submenu:eq(1)', $(this).parent().parent()).trigger('click');
                break;
        }

    });
    
    if (!hash) {
        if ($('a#album-'+idg, nav).length) {
            displayPresentation(false);
            displayPhotos(true);
            $('a#album-'+idg, nav).trigger('click');
        }
    } else {
        if (hash > 3) {
            displayPresentation(false);
            if (hash >= 4 && hash <= ($('.coda-nav ul li', nav).length - 4)) {
                displayPhotos(true);
            }

            if (hash >= ($('.coda-nav ul li', nav).length - 2)) {
                displayContacts(true);
            }
        }
    }

    if ($('a#album-'+idg, nav).length > 0) {
        $.addViewedAlbum(idg);
    }

    $('a.submenu').click(function(){
        if ($(this).attr('id') != 'm-photos') {
            $.addViewedAlbum($(this).attr('id').replace('album-',''));
        }
    });

    function displayPhotos(val){
        var _i = 5;
        var _txt = '';

        if(val){
            while(_boolPhotos==false){
                _txt = $.trim( $('.tab'+_i+' a', nav).attr('id') );
                if( 'm-actualites' == _txt ){
                    _boolPhotos = true;
                    return false;
                }else{
                    _dis_menu = true;
                    $('.tab'+_i, nav).css('display','block');
                }
                _i++;
            }
        } else {
            while(_boolPhotos==true){
                _txt = $.trim( $('.tab'+_i+' a', nav).attr('id') );
                if( 'm-actualites' == _txt ){
                    _boolPhotos = false;
                    return false;
                } else{
                    _dis_menu = false;
                    $('.tab'+_i, nav).css('display','none');
                }
                _i++;
            }
        }
    }

    function displayPresentation(val) {
        if(val){
            $('.tab2,.tab3', nav).show();
        } else {
            $('.tab2,.tab3', nav).hide();
        }
    }

    function displayContacts(val) {
        if(val){
            $('#lim-acess').show();
        } else{
            $('#lim-acess').hide();
        }
    }
});